package com.ssii.www.myproject.web.controller;

import com.ssii.www.myproject.common.util.AjaxResult;
import com.ssii.www.myproject.common.util.LogUtils;
import com.ssii.www.myproject.domain.Permission;
import com.ssii.www.myproject.domain.Role;
import com.ssii.www.myproject.query.BaseQuery;
import com.ssii.www.myproject.query.RoleQuery;
import com.ssii.www.myproject.service.RoleService;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController {
  @Autowired
  private RoleService roleService;

  private final static Logger logger = LogUtils.getLogger();

  @RequestMapping("/index")
  @RequiresPermissions("role:page")
  public String index() {
    return "role/index";
  }

  @RequestMapping("/page")
  @ResponseBody
//  @RequiresPermissions("role:page")
  public Map<String, Object> page(RoleQuery query) {
    Map<String, Object> map = new HashMap<>();
    Page<Role> pageByQuery = roleService.findPageByQuery(query);
    map.put("rows", pageByQuery.getContent());
    map.put("page", pageByQuery.getTotalPages());
    map.put("total", pageByQuery.getTotalElements());
    return map;
  }

  @PostMapping("/save")
  @ResponseBody
  public AjaxResult save(@RequestBody Role role) {
    roleService.save(role);
    return new AjaxResult();
  }

  @RequestMapping("/delete")
  @ResponseBody
  public AjaxResult delete(Long id) {
      roleService.delete(id);
      return new AjaxResult();
  }

}
